package com.atguigu.gulimall.member.interceptor;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @title: LoginUserInterceptor
 * @Author syh
 * @Date: 2022/7/12 17:58
 * @Version 1.0
 */

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // order/order/status/{orderSn} 设置订单如果有远程调用服务是以order/order开头的,就直接放行
//        String uri = request.getRequestURI();
//        boolean match = new AntPathMatcher().match("/member/**", uri);
//        boolean match1 = new AntPathMatcher().match("/member/member/oauth2/login", uri);
//        if (match || match1) {
//            // 如果请求路径匹配配置的路径就直接放行
//            return true;
//        }

        MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
//        if (attribute != null) {
//            loginUser.set(attribute);
//        } else {
//            // 没登录就去登录
//            request.getSession().setAttribute("msg", "请先进行登录");
//            response.sendRedirect("http://auth.gulimall.com/login.html");
//            return false;
//        }

        return true;
    }
}
